<template>
  <div>
  <van-form @submit="onSubmit">
  <van-field
    v-model="form.title"
    name="标题"
    label="标题"
    placeholder="标题"
    :rules="[{ required: true, message: '标题' }]"
  />
  <van-field
    v-model="form.content"
    name="内容"
    label="内容"
    placeholder="内容"
    :rules="[{ required: true, message: '内容' }]"
  />

  文件上传<van-uploader :after-read="afterRead" :before-read='beforeRead'/>
  <div>


  </div>

  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">提交</van-button>
  </div>
</van-form>
  </div>
</template>

<script>

import axios from "axios"
export default {
  data() {
    return {
      count:0,
      tcount:0,
      filename:'',
      file:'',
     form:{'cateid':1,'imgurl':'1.jpg','title':'','content':'','userid':1},
     lang: 'zh',
      searchword: '',
      tsize:0
    };
  },
  methods: {
    onSubmit(values) {
        axios.post("publishVedio/",this.form).then(res=>{

      })
    },
    beforeRead(file){
      this.file = file
      this.filename = file.name
      //计算文件总片数
      this.tsize = 50*1024*1024
      if(file.size>this.tsize){
         this.tcount = Math.ceil(file.size/this.tsize)
      }else{
        this.tcount = 1
      }
     
      alert(this.tcount)
      return true
    },
    async uploadf(data){
       await axios.post('http://localhost:8000/fileupload/',data).then(res=>{
          
            // this.form.imgurl = res.data.url
            // if(res.data.code == 200){
            //   alert("上传成功")
            // }else{
            //     // this.count++
            //     // this.afterRead(file)
            // }
        })
    },
    afterRead(file){
        var data = new FormData()
        if(this.tcount > 1){
            var start = this.count *(50*1024*1024)
            var end= (this.count+1)*(50*1024*1024)
            // 0  1024*1024
            // 1024*1024  2*1024*1024
            //当前上传的文件流
            data.append("file",this.file.slice(start,end))
        }else{
            data.append("file",file.file)
        }
        
        //文件名
        data.append("filename",this.filename)
        //一共有多少片
        data.append("tcount",this.tcount)
        //当前上传的索引
        data.append("count",this.count)
        data.append("size",this.file.size)
        

       
        this.uploadf(data)
       
        if(this.count == this.tcount-1){
            alert('上传成功')
        }else{
            this.count++
            this.afterRead(file)
        }
    },
    
  },
};
</script>

<style>

</style>